package com.song.user.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.user.entity.RoleInfo;
import com.song.user.service.RoleInfoService;
import com.song.common.result.ResultEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.Serializable;
/**
* <p>
    * 角色信息 控制器
    * </p>
*
* @author song
* @since 2024-06-05
*/
@Api(tags = {"角色信息 控制器"})
@RestController
@RequestMapping("/role_info")
public class RoleInfoController {

    @Autowired
    RoleInfoService roleInfoService;

    /**
    * 分页查询数据
    *
    * @author song
    * @Date 2024-06-05
    */
    @ApiOperation(value = "按条件分页检索角色信息",notes = "按条件分页检索角色信息")
    @GetMapping(value = "/pageList")
    public ResultEntity<Page<RoleInfo>> pageList(
        @Validated RoleInfo roleInfo,
        @ApiParam(value = "页码", defaultValue = "1")
        @RequestParam(defaultValue = "1") int pageNum,
        @ApiParam(value = "分页大小", defaultValue = "10")
        @RequestParam(defaultValue = "10") int pageSize
    ) {
        Page<RoleInfo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<RoleInfo> lambda = new QueryWrapper<RoleInfo>().lambda();
        if(ObjectUtils.isNotEmpty(roleInfo.getJs())){
            lambda.eq(RoleInfo::getJs,roleInfo.getJs());
        }
        if(ObjectUtils.isNotEmpty(roleInfo.getJsmc())){
            lambda.like(RoleInfo::getJsmc,roleInfo.getJsmc());
        }
        if(ObjectUtils.isNotEmpty(roleInfo.getJsms())){
            lambda.like(RoleInfo::getJsms,roleInfo.getJsms());
        }
        if(ObjectUtils.isNotEmpty(roleInfo.getJszt())){
            lambda.eq(RoleInfo::getJszt,roleInfo.getJszt());
        }
        return ResultEntity.success(roleInfoService.page(page,lambda));
    }

    /**
    * 角色信息新增
    *
    * @author song
    * @Date 2024-06-05
    */
    @ApiOperation(value = "新增角色信息",notes = "新增角色信息")
    @PostMapping(value = "/add")
    public ResultEntity add(@RequestBody RoleInfo roleInfo) {
        RoleInfo dbData = roleInfoService.getOne(new QueryWrapper<RoleInfo>().lambda().eq(RoleInfo::getJs, roleInfo.getJs()));
        if (dbData != null){
            return ResultEntity.failMessage("角色已存在");
        }
        roleInfo.setJszt("1");
        roleInfoService.save(roleInfo);
        return ResultEntity.success();
    }

    /**
    * 角色信息修改
    *
    * @author song
    * @Date 2024-06-05
    */
    @ApiOperation(value = "修改角色信息",notes = "修改角色信息")
    @PostMapping(value = "/update")
    public ResultEntity edit(@RequestBody RoleInfo roleInfo) {
        roleInfoService.updateById(roleInfo);
        return ResultEntity.success();
    }

    /**
    * 删除角色信息
    *
    * @author song
    * @Date 2024-06-05
    */
    @ApiOperation(value = "按id删除角色信息",notes = "按id删除角色信息")
    @PostMapping(value = "/delete")
    public ResultEntity delete(@RequestParam String id) {
        roleInfoService.removeById(id);
        return ResultEntity.success();
    }


    /**
    * 根据id查询角色信息
    *
    * @author song
    * @Date 2024-06-05
    */
    @ApiOperation(value = "根据id查询角色信息",notes = "根据id查询修改角色信息")
    @GetMapping(value = "/detail")
    public ResultEntity<RoleInfo> detail(@RequestParam String id) {
        RoleInfo roleInfo = roleInfoService.getById(id);
        return ResultEntity.success(roleInfo);
    }
}